<?php
class Error{
	
	public static function insertError($errorName,$errorText,$errorArt) {
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$errorTbl."
			SET
				errorName = '".DB::parseVar($errorName)."',
				errorText = '".DB::parseVar($errorText)."',
				errorArt = '".DB::parseVar($errorArt)."',
				errorDate = '".time()."'");
	}
	
	public static function getAllErrors() {
		$query = DB::getInstance()->query("
			SELECT
				errorid,
				errorName,
				errorText,
				errorDate,
				errorArt
			FROM
				".Tables::$errorTbl."
			ORDER BY
				errorDate DESC");
		$errors = array();
		while($e = DB::getInstance()->fetchArray($query)) {
			$e['date'] = General::formatDate($e['errorDate']);
			$errors[] = $e;
		}
		return $errors;
	}
	
	public static function getErrorCount() {
		$query = DB::getInstance()->query("
			SELECT
				errorid
			FROM
				".Tables::$errorTbl."");
		return DB::getInstance()->numRows($query);
	}

	
	public static function getErrors($min,$max,$kind = "") {
		if(!is_numeric($min) || !is_numeric($max)) exit;
		
		$whereClause = "";
		if($kind != "") {
			$whereClause = "WHERE errorArt = '".DB::parseVar($kind)."'";
		}
		
		$query = DB::getInstance()->query("
			SELECT
				errorid,
				errorName,
				errorText,
				errorDate,
				errorArt
			FROM
				".Tables::$errorTbl."
			".$whereClause."
			ORDER BY
				errorDate DESC
			LIMIT
				".$min.",".$max."");
		$errors = array();
		while($e = DB::getInstance()->fetchArray($query)) {
			$e['date'] = General::formatDate($e['errorDate']);
			$errors[] = $e;
		}
		
		return $errors;
	}
	
	
	public static function deleteError($id) {
		if(!is_numeric($id)) exit;
		
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$errorTbl."
			WHERE
				errorid = '".$id."'");
	}
	
	public static function deleteAllError() {
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$errorTbl."");
	}
}
?>